package com.shh.aiojbackendserver.interceptor;



import cn.dev33.satoken.stp.StpUtil;
import com.shh.aiojbackendserver.context.UserContext;
import com.shh.aiojbackendserver.model.entity.User;
import com.shh.aiojbackendserver.utils.JwtUtil;

import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * jwt令牌校验的拦截器
 */
@Component
@Slf4j
public class JwtInterceptor implements HandlerInterceptor {
    /**
     * 校验jwt
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        User user = (User) StpUtil.getSession().get("userInfo");
        UserContext.setUser(user); // 存入ThreadLocal
        return true;
    }
}
